package com.widetrust.carpro.dao.usermanage;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.widetrust.carpro.dao.test.UserDao;
import com.widetrust.carpro.domain.test.User;
import com.widetrust.carpro.domain.usermanage.AgentBean;
import com.widetrust.carpro.domain.usermanage.UserManageBean;
import com.widetrust.carpro.domain.usermanage.UserTypeBean;
import com.widetrust.carpro.domain.userright.Users;
import com.widetrust.carpro.orm.RowMapperForSpringJDBC;
import com.widetrust.config.ConfigException;
import com.widetrust.config.sqlbeans.Statements;
import com.widetrust.helper.ResourceUtils;
import com.widetrust.orm.DataConnection;
import com.widetrust.orm.DataConnectionPool;
import com.widetrust.orm.DataStatement;
import com.widetrust.orm.OracleDataStatementImpl;
import com.widetrust.orm.SQLData;
import com.widetrust.orm.pageination.MySQL5Pageination;
import com.widetrust.orm.pageination.Pageination;
import com.widetrust.orm.pageination.PageinationBean;
import com.widetrust.transdata.BsFormTable;


public class UserManageDao extends SimpleJdbcDaoSupport implements IUserManageDao {
	
	public static Log log = LogFactory.getLog( UserManageDao.class );

	public boolean saveUser(UserManageBean userManageBean,String belongcomid) {
		
		Statements statements=ResourceUtils.getStatements();
		String sql=statements.getSQLStatement("insertUser");
		String[] args=new String[]{userManageBean.getUsern(),userManageBean.getUremarks(),userManageBean.getUtype(),userManageBean.getPasswd(),userManageBean.getUtel(),userManageBean.getUSRAGEIDS(),belongcomid};
		int[] typeArr = new int[ args.length ];
		Arrays.fill( typeArr, Types.VARCHAR );
		
		int result = getJdbcTemplate().update(sql,args,typeArr);
		
		if(result==1){
			return true;
		}else{
			return false;
		}
	}
	
	public List<AgentBean> readAgent() {
		
		//Statements statements=ResourceUtils.getStatements();
		String sql="select ageids,agentname from Iagent";

		List<AgentBean> agentList=getJdbcTemplate().query(
				sql,
			new RowMapperForSpringJDBC(AgentBean.class));
		
		
			return agentList;
		
		}
	
	public List<UserTypeBean> readUserType() {
		Statements statements=ResourceUtils.getStatements();
		String sql="select ids,TYPENAME,TYPENO from IUSERTYPE";

		List<UserTypeBean> readUserType=getJdbcTemplate().query(
				sql,
			new RowMapperForSpringJDBC(UserTypeBean.class));
		
		
			return readUserType;
		
		}


	public List<User> getUserLIst() {
		return null;
	}


	public BsFormTable findAllUser(UserManageBean usermanageBean,PageinationBean pageBean) throws ConfigException, SQLException{

		SQLData data = new SQLData();
		data.setSqlId( "userList" );
		
		List conditions = usermanageBean.usermanageQueryCondition();
		data.setConditions(conditions);
		
		Statements statements = ResourceUtils.getStatements();
		DataConnection dataConnection = new DataConnectionPool( this.getDataSource());
		DataStatement dataStatement = new OracleDataStatementImpl( statements,dataConnection ); 
		Pageination page = new MySQL5Pageination( dataStatement,pageBean );
		return page.selectTable(data); 
	
	}
	
	public List<Users> findAllPrinter() {
		Statements statements=ResourceUtils.getStatements();
		String sql="SELECT u.*,t.typename,a.agentname FROM iusers u LEFT OUTER JOIN iusertype t ON u.utype = t.typeno LEFT OUTER JOIN iagent a ON u.usrageids=a.ageids where TYPENO=4 or TYPENO=9";
		String[] args=new String[]{};
		List<Users> readPrinterList=getJdbcTemplate().query(
				sql,
			new RowMapperForSpringJDBC(Users.class));
		
		
			return readPrinterList;
		
		}
	
	public List<Users> findPrinterandcomid(String BELONGCOMID) {
		Statements statements=ResourceUtils.getStatements();
		String sql="SELECT u.*,t.typename,a.agentname FROM iusers u LEFT OUTER JOIN iusertype t ON u.utype = t.typeno LEFT OUTER JOIN iagent a ON u.usrageids=a.ageids where (TYPENO=4 or TYPENO=9) and BELONGCOMID like ?";
		String[] args=new String[]{"%,"+BELONGCOMID+",%"};
		
		List<Users> findPrinterandcomid=getJdbcTemplate().query(sql,args,new RowMapperForSpringJDBC(Users.class));
			return findPrinterandcomid;
		
		}
	
	public List<Users> findUser(String userid) {
		Statements statements=ResourceUtils.getStatements();
		String sql=statements.getSQLStatement("secrchuser");
		
		String[] args=new String[]{userid};
		List<Users> userLIst = getJdbcTemplate().query(
				sql, args,
			new RowMapperForSpringJDBC(Users.class));
		
		return userLIst;
	
	}
	

	public boolean deleteUser(String userid){
		

		Statements statements=ResourceUtils.getStatements();
		String sql=statements.getSQLStatement("delUser");
				
		
		String[] args=new String[]{userid};
		int result = getJdbcTemplate().update(sql,args);
		
	
		if(result==1){
			return true;
		}else{
			return false;
		}
		
    }

	public boolean updateUser(Users users,String belongcomid,String userid) {

		JdbcTemplate jdbcTemplate=getJdbcTemplate();
		String sql="update iusers set usern=?,passwd=?,utype=?,uremarks=?,utel=?,usrageids=?,belongcomid=? where userid=?";
		
		String[] args=new String[]{users.getUsern(),users.getPasswd(),users.getUtype(),users.getUremarks(),users.getUTEL(),users.getUSRAGEIDS(),belongcomid,userid};
		int result = getJdbcTemplate().update(sql,args);
	
		if(result==1){
			return true;
		}else{
			return false;
		}
	
	}

	public boolean updatePW(String PASSWD,String userid) {

		JdbcTemplate jdbcTemplate=getJdbcTemplate();
		String sql="update iusers set passwd=? where userid=?";
		
		String[] args=new String[]{PASSWD,userid};
		int result = getJdbcTemplate().update(sql,args);
		if(result==1){
			return true;
		}else{
			return false;
		}
	
	}
	
	public HashMap<String, Users> getUserMap(){
		
		String sql = "select * from iusers order by usern";
		List userList = getJdbcTemplate().query( sql,new RowMapperForSpringJDBC(Users.class));
		Users userBean = null;
		HashMap userMap = new HashMap<String, Users>();
		for (Iterator iterator = userList.iterator(); iterator.hasNext();) {
			userBean = (Users) iterator.next();
			userMap.put( userBean.getUsern(), userBean );
		}
		return userMap;
	}


}
